/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifal.supermercadobarateiro.bd;

import br.edu.ifal.supermercadobarateiro.modelo.Cliente;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Mikaely Anaeve
 */
public class BdCliente {

    public Cliente busca(int cod) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();
        String sql = "select * from CadastroCliente where cod = ?";
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setInt(1, cod);
        ResultSet rs = stmt.executeQuery();
        Cliente c1 = new Cliente();
        while (rs.next()) {

            c1.setNome(rs.getString("nome"));
            c1.setEmail(rs.getString("email"));
            c1.setCidade(rs.getString("cidade"));
            c1.setBairro(rs.getString("bairro"));
            c1.setRua(rs.getString("rua"));
            c1.setSenha(rs.getString("senha"));
            c1.setRg(rs.getInt("rg"));
            c1.setCep(rs.getInt("cep"));




        }
        rs.close();
        stmt.close();
        con.close();
        return c1;
    }

    public void altera(Cliente c) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();
        String sql = "update CadastroCliente set nome=?,cidade=? ,email=?,bairro=?,rua=?,senha=?,rg=?,cep=?"
                + " where cod=?";

        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setString(1, c.getNome());
        stmt.setString(2, c.getCidade());
        stmt.setString(3, c.getEmail());
        stmt.setString(4, c.getBairro());
        stmt.setString(5, c.getRua());
        stmt.setString(6, c.getSenha());
        stmt.setInt(7, c.getRg());
        stmt.setInt(8, c.getCep());
        stmt.setInt(9, c.getCod());
        
        // stmt.setDate(11, new java.sql.Date(c.getDataNascimento().getTimeInMillis()));

        stmt.execute();
        stmt.close();
        con.close();
    }

    public void insere(Cliente c) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();

        String sql = "insert into CadastroCliente  "
                + "(nome,cidade,email,bairro,rua,senha,rg,cep) "
                + "values (?,?,?,?,?,?,?,?)";
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setString(1, c.getNome());
        stmt.setString(2, c.getCidade());

        stmt.setString(3, c.getEmail());
        stmt.setString(4, c.getBairro());
        stmt.setString(5, c.getRua());
        stmt.setString(6, c.getSenha());
         stmt.setInt(7, c.getRg());
          stmt.setInt(8, c.getCep());
        //stmt.setDate(11, new java.sql.Date(c.getDataNascimento().getTimeInMillis()));

        stmt.execute();
        stmt.close();
        con.close();
    }

    public void remove(Cliente c) throws SQLException {

        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.abreConexao();
        String sql = " delete from CadastroCliente where cod= ?";
        PreparedStatement stmt = con.prepareStatement("delete "
                + "from CadastroCliente where cod=?");
        stmt.setInt(1, c.getCod());
        stmt.execute();
        stmt.close();



    }
}
